package com.kinyx.framework.db.configs;

import com.kinyx.framework.annotation.assist.CanNotNull;
import com.kinyx.framework.annotation.assist.ReturnNotNull;
import com.kinyx.framework.db.defines.INamingStrategy;
import com.kinyx.framework.utils.KStringUtils;

/**
 * 命名策略实现类
 */
public class NamingStrategy implements INamingStrategy {

	/** @return 约定的前缀 */
	protected String getMappingStrategyPrefix() { return "T_"; }
	/** @return 约定的后缀 */
	protected String getMappingStrategySuffix() { return "Entity"; }

	@Override
	@ReturnNotNull
	public String entityName2tableName(@CanNotNull final String entityName) {
		String result = entityName;
		final String prefix = this.getMappingStrategyPrefix();
		final String suffix = this.getMappingStrategySuffix();
		// 去除后缀
		if (!KStringUtils.isBlank(suffix) && entityName.endsWith(suffix)) {
			result = entityName.substring(0, entityName.length() - suffix.length());
		}
		// 驼峰转下划线
		result = KStringUtils.hump2underline(result);
		// 补充前缀
		return prefix + result;
	}

	@Override
	@ReturnNotNull
	public String fieldName2columnName(@CanNotNull final String fieldName) {
		return KStringUtils.hump2underline(fieldName);// 驼峰转下划线
	}

}
